package com.beagle.njddapp.ui.presenter;

import android.content.Intent;
import android.text.TextUtils;

import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BaseApplication;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.LoginBean;
import com.beagle.njddapp.bean.RoleBean;
import com.beagle.njddapp.bean.UserInfoBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.net.BaseHttpListResponse;
import com.beagle.njddapp.net.BaseHttpResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.activity.BasicUserInfoActivity;
import com.beagle.njddapp.ui.activity.ChooseRoleActivity;
import com.beagle.njddapp.ui.activity.MainActivity;
import com.beagle.njddapp.ui.activity.SplanshActivity;
import com.beagle.njddapp.ui.view.CHooseRoleView;
import com.beagle.njddapp.ui.view.LoginView;
import com.beagle.njddapp.utils.PrefsManager;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;

import okhttp3.RequestBody;

import static com.beagle.njddapp.core.UserService.ROLE;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class ChooseRolePresenter extends BasePresenter<CHooseRoleView> {
    private ApiService apiService;

    public ChooseRolePresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        apiService = retrofit.create(ApiService.class);

    }
    public void getRoleList(){
        apiService.getRole().compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseHttpListResponse<RoleBean>>() {
            @Override
            public void success(BaseHttpListResponse<RoleBean> roleBeanBaseHttpListResponse) {
                if (roleBeanBaseHttpListResponse!=null){
                    getView().showRoleList(roleBeanBaseHttpListResponse.data);
                }

            }

            @Override
            public void error(int code, String message) {

            }
        });
    }

    public void chooseRloe(int roleId) {

        Gson gson=new Gson();
        HashMap<String,Integer> paramsMap=new LinkedHashMap<>();
        paramsMap.put("roleId",roleId);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.selectRole(body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseHttpResponse>() {


            @Override
            public void success(BaseHttpResponse baseHttpResponse) {
                LogUtils.d("success");

                    PrefsManager.getInstance(BaseApplication.getInstance()).putInt(ROLE,roleId);
                    UserService.getInstance().setRole(roleId);
                    getView().chooseSuccess();

            }

            @Override
            public void error(int code, String message) {
                LogUtils.d("error");
                if (code==200108){
                    PrefsManager.getInstance(BaseApplication.getInstance()).putInt(ROLE,roleId);
                    UserService.getInstance().setRole(roleId);
                    getView().chooseSuccess();
                }

            }
        });
    }

    public void changeRloe(int roleId) {

        Gson gson=new Gson();
        HashMap<String,Integer> paramsMap=new LinkedHashMap<>();
        paramsMap.put("roleId",roleId);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.selectRole(body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseHttpResponse>() {


            @Override
            public void success(BaseHttpResponse baseHttpResponse) {
                LogUtils.d("success");
                getUserInfo("");

            }

            @Override
            public void error(int code, String message) {
                LogUtils.d("error");
                if (code==200108){
                    getUserInfo(message);
                }else {
                    getView().onError(message);
                }
            }
        });
    }


    public void getUserInfo(String message){
        apiService.getUserInfo().compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseHttpResponse<UserInfoBean>>() {
            @Override
            public void success(BaseHttpResponse<UserInfoBean> userInfoBeanBaseHttpResponse) {
                if (userInfoBeanBaseHttpResponse.data!=null){
                    UserService.getInstance().setUserInfoBean(userInfoBeanBaseHttpResponse.data);
                    if (TextUtils.isEmpty(message)){
                        getView().chooseSuccess();
                    }else {
                        getView().onError(message);
                    }
                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);

            }
        });
    }
}
